package com.atung.parking.mapstruct;

import com.atung.parking.entity.BusSuperUser;
import com.atung.parking.enums.SuperUserState;
import com.atung.parking.export.SuperAdminExportVO;
import com.atung.parking.response.page.SuperUserResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 */
@Component
@Mapper(componentModel = "spring")
public interface SuperUserConverter {

    @Mappings({
            @Mapping(target = "creatorTime", source = "creatorTime", qualifiedByName = "timestampToString"),
            @Mapping(target = "status", source = "status", qualifiedByName = "setStatus")
    })
    SuperUserResponse toResponse(BusSuperUser busSuperUser);
    @Mappings({
            @Mapping(target = "creatorTime", source = "creatorTime", qualifiedByName = "timestampToString"),
            @Mapping(target = "status", source = "status", qualifiedByName = "setStatus")
    })
    SuperAdminExportVO toExportVo(BusSuperUser busSuperUser);

    @Named("timestampToString")
    default String timestampToString(String timestamp) {
        if (timestamp == null || timestamp.isEmpty()) {
            return null;
        }
        try {
            long timeInMillis = Long.parseLong(timestamp);
            Date date = new Date(timeInMillis);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        } catch (NumberFormatException e) {
            // 如果timestamp不是有效的长整型数字字符串，返回null或抛出异常
            return null;
        }
    }

    @Named("setStatus")
    default String setStatus(Integer status) {
        String description = SuperUserState.getDescriptionByIndex(status);
        return description;
    }

}
